/*
 *   Copyright 2009 Sukharev Eduard <kraplax@mail.ru>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "nullbyte-engine.h"
#include "postitem.h"

#include <qjson/json_driver.hh>

#include <Plasma/DataContainer>

#include <QHttp>
 
NullByteEngine::NullByteEngine(QObject* parent, const QVariantList& args)
    : Plasma::DataEngine(parent, args)
{
    // We ignore any arguments - data engines do not have much use for them
    // Also makes both compiler and doxygen happy
    Q_UNUSED(args)

    httpConnection = new QHttp("welinux.ru");
    setMinimumPollingInterval(1000);
    //connect(httpConnection, SIGNAL(readyRead(const QHttpResponseHeader resp)), this, SLOT(readData()));
};

QStringList NullByteEngine::sources() const
{
    QStringList sourcesList;
    sourcesList.append("Posts");
    sourcesList.append("Comments");

    return sourcesList;
};
 
bool NullByteEngine::sourceRequestEvent(const QString &name)
{
    // We do not have any special code to execute the
    // first time a source is requested, so we just call
    // updateSourceEvent().
    return updateSourceEvent(name);
};
 
bool NullByteEngine::updateSourceEvent(const QString &name)
{
    //httpConnection->get("/js/r_get.php?pst&date");
    if (name == "Posts"){
    /*/----------------------------------
    //httpConnection->get("/js/r_get.php?pst&date");
        JSonDriver * driver = new JSonDriver;
    bool *status = false;
    //QVariant fetchedData = driver->parse(jsonString, status);
    QVariant fetchedData = driver->parse("{\"arr\":[{\"id\":\"691\",\"title\":\"Some test title...\",\"who\":\"Ubuntu\",\"url\":\"blog\\/20\\/\",\"rate\":5},{\"id\":\"690\",\"title\":\"Some other test title\",\"who\":\"Tips & tricks\",\"url\":\"blog\\/38\\/\",\"rate\":1}],\"num\":2}", status);

    if(!status){
        qDebug() << "Conversion succeeded!";
        QVariantMap postsMap = fetchedData.toMap();

        foreach(QVariant postArr, postsMap["arr"].toList()){
            PostItem post(postArr);
            qDebug() << "Here we go - formatted string inside DataEngine: " << post.getFormattedString();
            // QMap<QString, QVariant> post = postArr.toMap();
        }
//         qDebug() << post;

    }else{
        qDebug() << "Conversion failed!";
        return false;
    };
    //-------------------------------------*/
    };

    return true;
};

void NullByteEngine::readData()
{
    QString jsonString = httpConnection->readAll();
    JSonDriver * driver = new JSonDriver;
    bool *status = false;
    QVariant fetchedData = driver->parse(jsonString, status);
    //QVariant fetchedData = driver->parse("{\"arr\":[{\"id\":\"691\",\"title\":\"Some test title...\",\"who\":\"Ubuntu\",\"url\":\"blog\\/20\\/\",\"rate\":5},{\"id\":\"690\",\"title\":\"Some other test title\",\"who\":\"Tips & tricks\",\"url\":\"blog\\/38\\/\",\"rate\":1}],\"num\":2}", status);

    if(!status){
        qDebug() << "Conversion succeeded!";
        QVariantMap postsMap = fetchedData.toMap();

        foreach(QVariant postArr, postsMap["arr"].toList()){
            PostItem post(postArr);
            qDebug() << "Here we go - formatted string inside DataEngine: " << post.getFormattedString();
            // QMap<QString, QVariant> post = postArr.toMap();
        }
//         qDebug() << post;

    }else{
        qDebug() << "Conversion failed!";
        return;
    };
};

// The first argument must match
// the X-Plasma-EngineName in the .desktop file.
K_EXPORT_PLASMA_DATAENGINE(nullbyte, NullByteEngine)

#include "nullbyte-engine.moc"
